var divTarget = null;
var xmlhttp = false;

function setDiv(url, parameters,div) {

	var elemns = document.getElementById(div).innerHTML;
	
	elemns = trim(elemns);
	
	if(elemns == ""){
		makeRequest(url, parameters, div);
	}else {
		document.getElementById(div).innerHTML = "";
	}
}

function trim(str){
	
	return str.replace(/^\s+|\s+$/g,"");
	
}
function mostraDiv(this_div, next_div) {
	
	if (getStyleObject(this_div) && getStyleObject(next_div)) {
		changeObjectVisibility(this_div, "visible");
		changeObjectVisibility(next_div, "hidden");
	}
}
function includeDivInOther(this_div,other_div) {

	var element = document.getElementById(other_div);
	var content = document.getElementById(this_div).innerHTML;
	
	element.innerHTML = content;

}
/*
 * Funções para Esconder ou Mostrar um Elemento
 */
function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its id
	if (document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}

function changeObjectVisibility(objectId, newVisibility) {
	// get a reference to the cross-browser style object and make sure the
	// object exists
	var styleObject = getStyleObject(objectId);
	if (styleObject) {
		styleObject.visibility = newVisibility;
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
}
/**
 * 
 * AJAX Functions
 * 
 **/

function enviaConsulta(acao,params,elemento_result) {
	
	this.divTarget = elemento_result;
	
	var url = acao + "?" + params;
	//url = antiCacheRand(url);
	http_request = GetXmlHttpObject();
	
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	document.getElementById(elemento_result).innerHTML = "<image src='../img/load_e.gif'>";
	http_request.onreadystatechange = getContents;
	http_request.open('GET', url, true);
	http_request.setRequestHeader("Cache-Control", "no-cache");
	http_request.setRequestHeader("Pragma", "no-cache");
	http_request.send(null);
}

function makeRequest(url, parameters,div) {
	this.divTarget = div;

	url = url + ".jsp";
	//url = antiCacheRand(url);
	
	
	http_request = GetXmlHttpObject();
	
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	document.getElementById(div).innerHTML = "<div id='image'><image src='../img/load_e.gif'></div>";
	
	http_request.onreadystatechange = getContents;
	http_request.open('GET', url + parameters, true);
	http_request.setRequestHeader("Cache-Control", "no-cache");
	http_request.setRequestHeader("Pragma", "no-cache");
	http_request.send(null);
}

function GetXmlHttpObject() {

	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
		
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	return xmlhttp;
}

function getContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var result = http_request.responseText;
			if (trim(result) == "sucesso") {
				alert("Redirecionando!");
				window.location.href = "restrito/index.jsp";
				return true;
			}
			document.getElementById(divTarget).innerHTML = result;
		} else {
			document.getElementById(divTarget).innerHTML = http_request.status;
		}
	}
}
/*
 * Autor: Micox - Náiron J.C.G - micoxjcg@yahoo.com.br - elmicox.blogspot.com
 */
function antiCacheRand(aurl){
    var dt = new Date();
    if(aurl.indexOf("?")>=0){
        return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
    }else{ return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
}

function setDiv(url, parameters,div) {

	var elemns = document.getElementById(div).innerHTML;
	
	elemns = trim(elemns);
	
	if(elemns == ""){
		makeRequest(url, parameters, div);
	}else {
		document.getElementById(div).innerHTML = "";
	}
}

function trim(str){
	
	return str.replace(/^\s+|\s+$/g,"");
	
}
function mostraDiv(this_div, next_div) {
	
	if (getStyleObject(this_div) && getStyleObject(next_div)) {
		changeObjectVisibility(this_div, "visible");
		changeObjectVisibility(next_div, "hidden");
	}
}
function includeDivInOther(this_div,other_div) {

	var element = document.getElementById(other_div);
	var content = document.getElementById(this_div).innerHTML;
	
	element.innerHTML = content;

}
/*
 * Funções para Esconder ou Mostrar um Elemento
 */
function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its id
	if (document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}

function changeObjectVisibility(objectId, newVisibility) {
	// get a reference to the cross-browser style object and make sure the
	// object exists
	var styleObject = getStyleObject(objectId);
	if (styleObject) {
		styleObject.visibility = newVisibility;
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
}
function enviaIncricao(form,div){
	enviaFormAjax("../service",form,div);
}

function IsNumber2(evt,form,div) {
	var tamanho = document.getElementById("insc").value;
	
	var charCode = (evt.which) ? evt.which : event.keyCode;
	var num = document.getElementById("sp_msg");
	
	if (charCode >= 48 && charCode <= 57 || charCode < 20) {

		if(tamanho.length == 8){
			if(charCode==13){
				enviaIncricao(form,div);
				document.getElementById("insc").value = "";
			}
			return false;
		}
		if(charCode==13){ 
			return false;
		}
		num.innerHTML = "Informe a inscrição:789XXXXX";
		return true;
	
		
	}else {
		num.innerHTML = "Somente Numeros!";
		return false;
	}
}